<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//产品品类
class Productscategory extends Common
{
	public function huishou($page=1,$list=10){
		$where=["c.is_actived"=>0,"c.is_deleted"=>0,"b.is_actived"=>1,"b.is_deleted"=>0];

		$sum=Db::table("pro_category")->alias("c")->join("pro_brands b","c.brand_id=b.id")->where($where)->count();
		$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        //echo ROOT_PATH;

       	$datapc=Db::table("pro_category")->alias("c")->join("pro_brands b","c.brand_id=b.id")
        ->join("users uc","c.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","c.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)
        ->order("c.id")
        ->limit($start,$list)
        ->field("c.id,b.brand_name,c.category_name,ec.em_name as cname,c.created_on,em.em_name as mname,c.modifiled_on")
        ->select();

        for($i=0;$i<count($datapc);$i++){
        	$datapc[$i]["created_on"]=date("Y-m-d H:i",strtotime($datapc[$i]["created_on"]));
        	if($datapc[$i]["modifiled_on"])
        		$datapc[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($datapc[$i]["modifiled_on"]));
        	
        }
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("datapc",$datapc);
		return $this->fetch('huishou');
	}
	public function category($page=1,$list=10){

		$where=["c.is_actived"=>1,"c.is_deleted"=>0,"b.is_actived"=>1,"b.is_deleted"=>0];

		$sum=Db::table("pro_category")->alias("c")->join("pro_brands b","c.brand_id=b.id")->where($where)->count();
		$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;
        //echo ROOT_PATH;

       	$datapc=Db::table("pro_category")->alias("c")->join("pro_brands b","c.brand_id=b.id")
        ->join("users uc","c.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","c.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)
        ->order("c.id")
        ->limit($start,$list)
        ->field("c.id,b.brand_name,c.category_name,ec.em_name as cname,c.created_on,em.em_name as mname,c.modifiled_on")
        ->select();

        for($i=0;$i<count($datapc);$i++){
        	$datapc[$i]["created_on"]=date("Y-m-d H:i",strtotime($datapc[$i]["created_on"]));
        	if($datapc[$i]["modifiled_on"])
        		$datapc[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($datapc[$i]["modifiled_on"]));
        	
        }
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("datapc",$datapc);
		return $this->fetch('category');
	}

	public function new_category(){

		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        //id:$("#isupdate").val(),category_name:$("#category_name").val(),bran_id:$("#bran_id").val()
        $id=$datas["id"];$category_name=trim(htmlspecialchars($datas["category_name"]));$brand_id=trim($datas["bran_id"]);
        if(!$category_name)
        	return 2;//品类名空
        if(!$brand_id)
        	return 3;//产品空
        if($id){
        	//修改
        	$iscunzai=Db::table("pro_category")->where("category_name='{$category_name}' and is_deleted=0 and id<>$id")->count();
			if($iscunzai)
				return 4;//重名
			$data["id"]=$id;
			$data["category_name"]=$category_name;
			$data["brand_id"]=$brand_id;
			$data["m_users_id"]=Session::get("login_id");
			$data["modifiled_on"]=date("Y-m-d H:i:s");
			Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_category")->update($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }
        }else{
        	$iscunzai=Db::table("pro_category")->where("category_name='{$category_name}' and is_deleted=0")->count();
			if($iscunzai)
				return 4;//重名
        	//创建
        	$data["category_name"]=$category_name;
        	$data["c_users_id"]=Session::get("login_id");
        	$data["created_on"]=date("Y-m-d H:i:s");
        	$data["brand_id"]=$brand_id;

        	Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_category")->insert($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }

        }

	}
	//获取品类
	public function getcate(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //$data["bid"]=2;
		$where=["c.id"=>$data["cid"]];
		$arr=Db::table("pro_category")->alias("c")
		->join("pro_brands b","c.brand_id=b.id")
		->where($where)->field("c.id,c.category_name,b.id as bid,b.brand_name")->find();
		return json_encode($arr);
	}
	//删除品类
	public function dropcate(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_category")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复品类
	public function huifu(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_category")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
  //获取品类树状图
  public function getcatelist(){
      $request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["cid"]=2;
        $result=[];
        $where=["is_actived"=>1,"is_deleted"=>0];
        $res=Db::table("pro_brands")->where($where)->select();
         for($i=0;$i<count($res);$i++){
                $result[$i]["id"]=$res[$i]["id"];
               
                $result[$i]["name"]=$res[$i]["brand_name"];
                $result[$i]["pId"]=0;
                $result[$i]["nocheck"]=true;
                
           }
           $res2=Db::table("pro_category")->where($where)->select();
            for($i=0;$i<count($res2);$i++){
             
                $result[$i+count($res)]["eid"]=$res2[$i]["id"];
               
                 $result[$i+count($res)]["name"]=$res2[$i]["category_name"];
                $result[$i+count($res)]["pId"]=$res2[$i]["brand_id"];
                if($data["cid"]==$res2[$i]["id"]){
                  $result[$i+count($res)]["checked"]=true;
                }
                
           }
            return json_encode($result);
  }
}